$(function () {
    loadAlarmInfo();
    loadEnterprise();
    // loadDevice();
})
const title = ['#', '告警等级', '企业', '设备', '告警内容', '告警时间', '处理状态', '处理结果'];
const label = ['order', 'grade', 'ename', 'name', 'content', 'createtime', 'status', 'result'];

function loadAlarmInfo(_pageIndex) {
    $('#alarmList tbody').empty();
    let pageIndex = parseInt(_pageIndex ? _pageIndex : '1');
    showLoading(LOADING);
    getJson("/alarm/findAlarmByParams", {
        eId: $('#company-list').val(),
        dId: $('#device-list').val(),
        beginTime: $('#searchTimeBegin').val(),
        endTime: $('#searchTimeEnd').val(),
        status: $('#status-id').val(),
        pageIndex: pageIndex,
        pageSize: 10
    }, 'GET', function (result) {
        if (result.code == 200) {
            getJson("/alarm/findCountAlarm", {
                eId: $('#company-list').val(),
                dId: $('#device-list').val(),
                beginTime: $('#searchTimeBegin').val(),
                endTime: $('#searchTimeEnd').val()
            }, 'GET', function (result) {
                for (let i = 0; i < result.length; i++) {
                    if (result[i].name == 1) {
                        $('#span-id3').html(result[i].value);
                    } else if (result[i].name == 2) {
                        $('#span-id4').html(result[i].value);
                    } else if (result[i].name == 3) {
                        if (result[i].value > 0) {
                            $('#span-id1').html(result[i].value);
                        }
                    } else if (result[i].name == 4) {
                        if (result[i].value > 0) {
                            $('#span-id2').html(result[i].value);
                        }
                    }
                }
            })
            let data = result['payload']['data'];
            setPage($('#page'), pageIndex, result['payload']['pages'], result['payload']['total'], function (num) {
                loadAlarmInfo(num);
            });
            let order = 1 + 10 * (pageIndex - 1);
            for (let item of data) {
                item['order'] = order++;
            }
            buildTable($('#alarmList'), title, label, data, function (td, label, item) {
                switch (label) {
                    case 'grade':
                        $(td).empty();
                        let delSpan = createElement('span', td, null, null);
                        switch (item['grade']) {
                            case 1:
                                $(delSpan).addClass('badge level1');
                                $(delSpan).text('普通');
                                break;
                            case 2:
                                $(delSpan).addClass('badge level2');
                                $(delSpan).text('一般');
                                break;
                            case 3:
                                $(delSpan).addClass('badge level3');
                                $(delSpan).text('严重');
                                break;
                            case 4:
                                $(delSpan).addClass('badge level4');
                                $(delSpan).text('故障');
                                break;
                        }
                        break;
                    case 'status':
                        let statusType = item['status'] == 1 ? '未处理' : item['status'] == 2 ? '未恢复' : '已恢复';
                        $(td).text(statusType);
                        break;
                    case 'result':
                        let res = item['result'] == null ? '' : item['result'].result;
                        $(td).text(res);
                        break;
                }
            })
            hideLoading();
        } else {
            hideLoading();
            myAlert("提示", result.message)
        }
    })
}

/*根据企业加载设备列表*/
function loadDeviceByEid(e) {
    $("#device-list").empty();
    $(new Option("选择设备", "0")).appendTo($("#device-list"));
    getJson("/device/getDeviceByEId", {"id": e}, 'GET', function (data) {
        for (let i = 0; i < data.length; i++) {
            $(new Option(data[i].name, data[i].id)).appendTo($("#device-list"));
        }
    })
}

/*加载企业信息下拉框*/
function loadEnterprise() {
    getJson("/enterprise/findEnterpriseS", null, 'GET', function (result) {
        if (result.code == 200) {
            let data = result.payload.data;
            for (let i = 0; i < data.length; i++) {
                $(new Option(data[i].name, data[i].id)).appendTo($("#company-list"));
            }
        } else {
            myAlert("提示", result.message);
        }
    })
}